package com.meow_woof.service.impl;

import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.meow_woof.dto.Result;
import com.meow_woof.entity.ShopType;
import com.meow_woof.mapper.ShopTypeMapper;
import com.meow_woof.service.IShopTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.List;

import static com.meow_woof.utils.RedisConstants.CACHE_SHOPTYPE_KEY;

/**
 * 服务实现类
 */
@Service
public class ShopTypeServiceImpl extends ServiceImpl<ShopTypeMapper, ShopType> implements IShopTypeService {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    public Result queryTypeList() {
        String key=CACHE_SHOPTYPE_KEY;
        String shopTypeCache=stringRedisTemplate.opsForValue().get(key);
        if(StrUtil.isNotBlank(shopTypeCache)){
            List<ShopType> typeList= JSONUtil.toList(shopTypeCache, ShopType.class);
            return Result.ok(typeList);
        }
        List<ShopType> typeList = query().orderByAsc("sort").list();
        if (typeList.isEmpty()){
            return Result.fail("无店铺类型");

        }
        stringRedisTemplate.opsForValue().set(key,JSONUtil.toJsonStr(typeList));
        return Result.ok(typeList);

    }
}
